package com.dkm.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dkm.entity.Trace;
import com.dkm.service.TraceService;
import com.dkm.util.LayuiJsonUilt;
import com.dkm.util.ResultUtil;

@RestController
@RequestMapping("/trace")
public class TraceController {

	//注入跟踪业务层
	@Autowired
	private TraceService traceService;
	
	//多条件查询跟踪日志
	@GetMapping
	@RequiresPermissions(value = "trace:query")
	public LayuiJsonUilt<Trace> findAllTraces(String sid,Trace trace,LayuiJsonUilt<Trace> lj) {
		Map<String, Object> map=new HashMap<String, Object>();
		//学生编号
		if(sid!=null&&sid!="") {
			map.put("sid", sid);
		}
		
		if(trace!=null) {
			map.put("trace", trace);
		}
		
		Direction direction=Sort.Direction.DESC;
		Sort sort=JpaSort.unsafe(direction, "\\Qcreate_time\\E");
		//创建分页对象
		Pageable pageable=PageRequest.of(lj.getPage(), lj.getLimit(),sort);
		//获取jpa的page对象
		Page<Trace> page=traceService.findAllTraces(map, pageable);
		
		//设置数据和数据长度
		lj.setData(page.getContent());
		lj.setCount(page.getTotalElements());
		return lj;
	}
	
	//添加跟踪信息
	@PostMapping
	@RequiresPermissions(value = "trace:add")
	public ResultUtil addTrace(String user_id,String sid,Trace trace) {
		
		return traceService.addTrace(user_id,sid,trace);
	}
}
